package com.aaa.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

@Configuration
public class RabbitMQConfig {

    public static final String EXCHANGE_NAME = "bill.exchange"; // 交换机名称
    public static final String QUEUE_SAVE_UPDATE = "bill.saveupdate"; // 保存修改队列
    public static final String ROUTING_SAVE_UPDATE_KEY = "bill.saveupdatekey"; // 保存修改路由键
    public static final String ROUTING_DELETE_KEY = "bill.deletekey"; // 删除路由键

    /**
     * 添加/修改定义队列
     * @return 队列对象
     */
    @Bean
    public Queue queueSaveUpdate() {
        System.out.println(QUEUE_SAVE_UPDATE + "：RabbitMQ队列初始化成功：" + LocalDateTime.now());
        return new Queue(QUEUE_SAVE_UPDATE, true); // durable: 是否持久化
    }


    /**
     * 定义交换机
     * @return 交换机对象
     */
    @Bean
    public TopicExchange exchange() {
        System.out.println(EXCHANGE_NAME + "：RabbitMQ交换机初始化成功：" + LocalDateTime.now());
        return new TopicExchange(EXCHANGE_NAME);
    }

    /**
     * 绑定队列和交换机
     * @return 绑定对象
     */
    @Bean
    public Binding bindingSaveUpdate() {
        System.out.println(ROUTING_SAVE_UPDATE_KEY + "：RabbitMQ绑定队列和交换机成功：" + LocalDateTime.now());
        return BindingBuilder.bind(queueSaveUpdate()).to(exchange()).with(ROUTING_SAVE_UPDATE_KEY);
    }

}
